﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text;
using common;
using common.web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public partial class api_upfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string _t = Request["t"];
        Response.Write(getData(_t));
    }

    string getData(string t)
    {
        switch (t)
        {
            case "up":
                return up();          //上传
        }
        return "缺少参数!";
    }

    string up()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 2);
            jo.Add("Message", "uid错误");
            return JsonConvert.SerializeObject(jo);
        }
        string[] fm = wjsc(_uid, "mc");
        if (fm[0] != "0")
        {
            jo.Add("Result", 2);
            jo.Add("Message", "上传错误");
            return JsonConvert.SerializeObject(jo);
        }
        string mc = func.safeStr(Req("mc"));
        //string sfz = func.safeStr(Req("sfz"));
        //string yhk = func.safeStr(Req("yhk"));
        //string khyh = func.safeStr(Req("khyh"));
        //string shen = func.safeStr(Req("shen"));
        //string shi = func.safeStr(Req("shi"));
        //string zhihang = func.safeStr(Req("zhihang"));

        //if (xm == "" || sfz == "" || yhk == "")
        //{
        //    jo.Add("Result", 2);
        //    jo.Add("Message", "参数错误");
        //    return JsonConvert.SerializeObject(jo);
        //}
        //db.ExecNQ("update tb_Sale set cName='" + xm + "',cIDCard='" + sfz + "',cBankCard='" + yhk + "',cKhyh='" + khyh + "',cShen='" + shen + "',cShi='" + shi + "',cZhihang='" + zhihang + "',cFrontPic='" + zm[1] + "',cBackPic='" + fm[1] + "',tBcsj=getdate(),fState=1 where id=" + _uid + " and fState in (0,3)");

        db.ExecNQ("");
        HttpContext.Current.Response.Write("<script type=\"text/javascript\">alert(\"保存成功!\");parent.location.href='/';</script>");
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    string[] wjsc(string _uid, string mc)
    {
        HttpPostedFile pf = HttpContext.Current.Request.Files[mc];
        //判断上传文件是不是JPG格式           
        BinaryReader br = new BinaryReader(pf.InputStream);
        byte[] buffer = br.ReadBytes(2);
        string fileClass = buffer[0].ToString() + buffer[1].ToString();
        if (fileClass != "208207" && fileClass != "8075")
        {
            br.Dispose();
            return new string[] { "1", "只能上传xls.doc.ppt.xlsx,zip,pptx,mmap,zip格式!" };
        }
        if (pf.ContentLength > 4000 * 1024)
        {
            return new string[] { "1", "图片太大，图片必须小于4M!" };
        }
        //filename = Path.GetExtension(pf.FileName).ToLower();
        //chkkzm(filename); //验证文件类型
        Random rd = new Random();
        string filename = _uid + rd.Next(1000, 9999) + pf.ContentType;
        pf.SaveAs(HttpContext.Current.Server.MapPath("/picfile/") + filename);
        br.Dispose();
        return new string[] { "0", filename };
    }
    /*文件扩展名说明
            *7173        gif 
            *255216      jpg
            *13780       png
            *6677        bmp
            *239187      txt,aspx,asp,sql
            *208207      xls.doc.ppt
            *6063        xml
            *6033        htm,html
            *4742        js
            *8075        xlsx,zip,pptx,mmap,zip
            *8297        rar   
            *01          accdb,mdb
            *7790        exe,dll           
            *5666        psd 
            *255254      rdp 
            *10056       bt种子 
            *64101       bat 
            */

    protected string getUid()
    {
        string _uid = getID(HttpUtility.UrlDecode(func.NoNull(Req("uid")).Replace("+", "%2b")));
        if (_uid == "0")
            _uid = pg.usid();
        return _uid;
    }

    /// <summary>
    /// 解密id
    /// </summary>
    protected string getID(string OpenID)
    {
        int id = 0;
        if (!string.IsNullOrEmpty(OpenID) && int.TryParse(new KeyPwd().Decrypto(OpenID), out id))
            return id.ToString();
        else
            return "0";
    }

    protected string Req(string s)
    {
        return HttpContext.Current.Request[s];
    }

}